package com.example.demo.controller;

import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.TokenData;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 修改密码 (√)
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @PostMapping(value = "/updatePassword")
    public Result updatePassword(String oldPassword, String newPassword, HttpSession session){
        TokenData currentUser = (TokenData)session.getAttribute("currentUser");
        Result result = this.userService.updatePasword(currentUser.getReader_id(), oldPassword, newPassword);
        return result;
    }


    /**
     * 添加管理员 (√)
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/addAdmin")
    public Result addAdmin( User user,HttpSession session){
        TokenData  tokenData =(TokenData) session.getAttribute("currentUser");
        if(tokenData.getUser_state() == 0){  //超级管理员

            user.setUser_state(1);
            user.setState(1);
            Result result = this.userService.saveUser(user);

            return result;

        }else if(tokenData.getUser_state() == 1){  //管理员添加读者
            user.setUser_state(2);
            user.setState(1);
            Result result = this.userService.saveUser(user);

            return result;
        }
        return ResultFactory.failure("你没有此权限");
    }

    /**
     * 读者列表展示
     */
    @GetMapping("/readerList")
    public Result queryUserList(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "2") int pageSize){

        PageBean<User> userPageBean = this.userService.selAllUser(currentPage, pageSize, 2);
        return  ResultFactory.successWithStatusAndData(200,"读者列表展示",userPageBean);
    }

    /**
     * 管理员列表展示 (√)
     */
    @GetMapping("/adminList")
    public Result queryAdminList(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "2") int pageSize){

        PageBean<User> userPageBean = this.userService.selAllUser(currentPage, pageSize, 1);
        return  ResultFactory.successWithStatusAndData(200,"读者列表展示",userPageBean);
    }

    /**
     * 读者查询
     */
    @GetMapping("/queryReaderByConditions")
    public Result queryReaderByConditions(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "2") int pageSize,User user){
        PageBean<User> userPageBean = this.userService.selAllUserByConditions(currentPage, pageSize, user,2);

        if(userPageBean.getTotalNum() == 0){
            return  ResultFactory.failureWithStatus(999,"没有此用户的相关信息");
        }
        return  ResultFactory.successWithStatusAndData(200,"查找结果",userPageBean);
    }

    /**
     * 管理员查询
     */
    @GetMapping("/queryAdminByConditions")
    public Result queryAdminByConditions(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "2") int pageSize,User user){
        PageBean<User> userPageBean = this.userService.selAllUserByConditions(currentPage, pageSize, user,1);

        if(userPageBean.getTotalNum() == 0){
            return  ResultFactory.failureWithStatus(999,"没有此用户的相关信息");
        }
        return  ResultFactory.successWithStatusAndData(200,"查找结果",userPageBean);
    }

    /**
     * 验证用户名是否存在
     * @param stunum
     * @return
     */
    @GetMapping("/checkstunum/{stunum}")
    public Result checkStunum(@PathVariable String stunum){
        Result result = this.userService.checkStunum(stunum);
        return result;
    }



}
